package com.lyxmaster.lib.bluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;

import com.lyxmaster.lib.toasty.Toasty;
import com.lyxmaster.lib.util.HudEx;
import com.lyxmaster.lib.view.BaseActivity;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

public class BtHelper {
	public interface OnConnectListener {
		void onSuccess();
		void onError(String strError);
	}

	public BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
	public BluetoothDevice btDevice;
	private BluetoothSocket btSocket;
	private OutputStream btOs;
	public InputStream btIs;
	private boolean bConn = false;

	private static volatile BtHelper btHelper;

	private BtHelper() {}

	public static BtHelper getInstance() {

		if (btHelper == null) {

			synchronized (BtHelper.class) {

				if (btHelper == null) {

					btHelper = new BtHelper();

				}
			}
		}

		return btHelper;

	}
	
	public void onConnBt(String strBtAddress) throws IOException
	{
		if(btSocket != null) btSocket.close();
		btDevice = btAdapter.getRemoteDevice(strBtAddress);
		btSocket = btDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
		btSocket.connect();
		btOs = btSocket.getOutputStream();
		btIs = btSocket.getInputStream();
		
		bConn = true;
	}

	public void connectBt(final Context context, final String name, final String mac, final OnConnectListener onConnectListener) {
		try {
            HudEx.show(context);

			new Thread() {
				boolean bConnect = false;
				String error = "";

				public void run() {
					//这儿是耗时操作，完成之后更新UI；
					try {
						BtHelper.getInstance().onConnBt(mac);
						bConnect = true;
					} catch (IOException e) {
						bConnect = false;
						error = e.toString();
					}
					((Activity) context).runOnUiThread(new Runnable() {
						@Override
						public void run() {
                            HudEx.dismiss();
							if (bConnect) {
								Toasty.success(context, "连接成功").show();
								Intent broadcastIntent = new Intent();
								broadcastIntent.setAction(BtService.BLUETOOTHCONNECT);
								context.sendBroadcast(broadcastIntent);    //发送广播给BtServier
								if(onConnectListener!=null)onConnectListener.onSuccess();
							} else {
								Toasty.error(context, "连接失败:" + error).show();
								if(onConnectListener!=null)onConnectListener.onError("连接失败:" + error);
							}

						}
					});
				}
			}.start();
		} catch (Exception e) {
			Toasty.error(context, e.toString()).show();
            HudEx.dismiss();
		}
	}
	
	public void onSend(byte[] data) throws IOException
	{
		if(btOs == null)
		{
			throw new IOException("设备未连接");
		}
		
		btOs.write(data);
	}
	
	public void onDisconnect() throws IOException
	{
		if(btSocket != null)
		{
			btSocket.close();
			btSocket = null;
		}
		
		bConn = false;
	}
	
	public boolean isConn()
	{
		return bConn;
	}
}
